<template>
  <el-dialog
    :title="'电子证明信息查询'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" v-loading="submitLoading"  :element-loading-text="'正在提交'"
     :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">

    <el-form-item label="登记中心" prop="djzx"   v-if="false"  >
      <el-select v-model="dataForm.djzx" placeholder="登记中心" style="width:100%">
          <el-option
            v-for="item in dataForm.bankList"
            :key="item.bankid"
            :label="item.bankname"
            :value="item.bankid">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="地市" prop="seRegion" v-if="id != -1"  :class="{ 'is-required': id != -1}" >
      <el-select  @change="selectChanged" v-model="dataForm.seRegion"
       placeholder="区域" style="width:100%">
          <el-option
            v-for="item in dataForm.seRegionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="区县" prop="region">
      <el-select v-model="dataForm.region" 
       :disabled="id == -1"
      placeholder="区县" style="width:100%">
          <el-option
            v-for="item in dataForm.regionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="业务标识码" prop="ywbsm" >
      <el-input v-model="dataForm.ywbsm"  :disabled="id == -1" placeholder="业务标识码"></el-input>
    </el-form-item>

    <el-form-item label="业务件号" prop="ywjh" >
      <el-input v-model="dataForm.ywjh"  :disabled="id == -1" placeholder="业务件号"></el-input>
    </el-form-item>

    <el-form-item label="登记证明号" prop="bdcdjzmh"  >
      <el-input v-model="dataForm.bdcdjzmh"  :disabled="id == -1" placeholder="登记证明号"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="submitLoading" >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {



      var validateseRegion = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('地市不能为空'))
        } else {
          callback()
        }
      }

      return {
        visible: false,
        submitLoading: false,
        id : '',
        dataForm: {
          djzx: '',
          region: '',
          seRegion: '',
          bdcdjzmh: '',
          ywjh: '',
          ywbsm: '',
          bankList: [],
          regionList: [],
          seRegionList: [],

        },
        dataRule: {
          djzx: [
            { required: true, message: '登记中心不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '区县不能为空', trigger: 'blur' }
          ],
          seRegion: [
            // { required: true, message: '地市不能为空', trigger: 'blur' }
             { validator: validateseRegion, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, djzx, region, servicecode, serviceno, realtyno) {
        this.id = id || 0
        this.visible = true

        this.dataForm.djzx = djzx || ''
        this.dataForm.region = region || ''
        this.dataForm.ywbsm = servicecode || ''
        this.dataForm.ywjh = serviceno || ''
        this.dataForm.bdcdjzmh = realtyno || ''

        this.$http({
          url: this.$http.adornUrl(`/bdc/bdccontract/item/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.seRegionList = data.regionList
              this.dataForm.contractTypeList = data.contractTypeList
                 this.dataForm.bankList = data.bankList
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })

      },

      selectChanged(value){
          this.dataForm.region = '';
          this.$http({
          url: this.$http.adornUrl(`/sys/bdcregion/listByLevle`),
              method: 'get',
              params: this.$http.adornParams({'upregion':value})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionList = data.list
            }
        })
      }
      ,

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {

          if (!this.dataForm.ywjh && !this.dataForm.bdcdjzmh && !this.dataForm.ywbsm){
            this.$message.error("业务标识码、不动产登记证明号、业务件号至少填一个")
            return ;
          }

          if (valid) {
            this.submitLoading = true
            this.$http({
              url: this.$http.adornUrl(`/bdc/bdccertinfo/send`),
              method: 'post',
              data: this.$http.adornData({
                // 'djzx': this.dataForm.djzx,
                'region': this.dataForm.region,
                'bdcdjzmh': this.dataForm.bdcdjzmh,
                'ywjh': this.dataForm.ywjh,
                'ywbsm': this.dataForm.ywbsm,
                'seRegion': this.dataForm.seRegion
 

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    if (this.id == -1){
                      this.$router.push({ name: 'bdc-bdccertinfo' })
                    }else{
                      this.$emit('refreshDataList')   
                    }
                    
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.submitLoading = false
            }).catch(err => {
                this.submitLoading = false;
                console.error(err);
                this.$message.error('系统异常')              
              }); 
          }
        })
      }
    }
  }
</script>
<style>
.el-input.is-disabled .el-input__inner {
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #313b50;
    cursor: not-allowed;
}
</style>